<template>
  <div class="app-container">
    <el-form
      ref="listFilterRef"
      :model="list.filter"
      inline
      class="list-filter"
    >
      <el-form-item prop="name">
        <el-input
          v-model="list.filter.name"
          placeholder="请输入活动名称搜索"
          clearable
        />
      </el-form-item>

      <el-form-item prop="status">
        <Status v-model="list.filter.status" />
      </el-form-item>

      <el-form-item>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-operation">
      <AuthButton @click="c" name="新增" />
      <el-pagination
        v-bind="elPaginationProps"
        :total="list.total"
        :currentPage.sync="list.filter.pageNo"
        :pageSize.sync="list.filter.pageSize"
      />
    </div>

    <el-table v-loading="list.loading" :data="list.data" v-bind="tableProp">
      <el-table-column align="center" type="index" />
      <el-table-column label="活动名称" prop="name" />
      <el-table-column label="活动ID" prop="id" />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="创建人" prop="createUserInfo.name" />
      <el-table-column label="备注说明" prop="description" />
      <el-table-column label="路径">
        <template slot-scope="{ row }">
          {{ `/pages/couponPKG/coupon/main?id=${row.id}` }}
        </template>
      </el-table-column>
      <el-table-column label="启用状态" width="80" align="center">
        <template slot-scope="{ row: { id, status } }">
          <KiPopSwitch
            v-bind="popSwitchProps(status)"
            @change="updateStatus({ id, status: status ^ 1 })"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="320">
        <template slot-scope="{ row }">
          <AuthButton @click="r({ id: row.id })" name="查看" />
          <AuthButton @click="u({ id: row.id })" name="编辑" />
          <AuthButton @click="createQrCode(row)" name="手机预览" />
          <AuthButton @click="toActivity(row)" name="活动场次" />
          <AuthButton @click="d({ id: row.id })" name="删除" />
        </template>
      </el-table-column>
    </el-table>

    <KiFormDialog
      :show.sync="form.show"
      :title="formTitle"
      v-model="form.data"
      :submit="submitForm"
      :readonly="form.status === 'r'"
      ref="formRef"
      :loading="form.loading"
    >
      <template #el-form>
        <div class="title">活动主页</div>
        <el-form-item label="活动名称" prop="name" verify>
          <el-input
            v-model="form.data.name"
            clearable
            maxlength="30"
            show-word-limit
            placeholder="请输入活动名称"
          />
        </el-form-item>
        <el-form-item label="备注说明" prop="description">
          <el-input
            v-model="form.data.description"
            type="textarea"
            clearable
            maxlength="100"
            show-word-limit
            placeholder="请输入备注说明"
          />
        </el-form-item>
        <div class="flex">
          <el-form-item
            class="flex-1"
            label="顶部banner"
            prop="activityTopBanner"
            verify
          >
            <Imgpond
              v-model="form.data.activityTopBanner"
              aspectRatio="375/484"
              :count="1"
            />
          </el-form-item>
          <el-form-item
            class="flex-1"
            label="标题banner"
            prop="activityTitleBanner"
            verify
          >
            <Imgpond
              v-model="form.data.activityTitleBanner"
              aspectRatio="375/120"
              :count="1"
            />
          </el-form-item>
        </div>
        <div class="title">场次主页</div>
        <div class="flex">
          <el-form-item
            class="flex-1"
            label="顶部banner"
            prop="sessionTopBanner"
            verify
          >
            <Imgpond
              v-model="form.data.sessionTopBanner"
              aspectRatio="375/484"
              :count="1"
            />
          </el-form-item>
          <el-form-item
            class="flex-1"
            label="标题banner"
            prop="sessionTitleBanner"
            verify
          >
            <Imgpond
              v-model="form.data.sessionTitleBanner"
              aspectRatio="375/120"
              :count="1"
            />
          </el-form-item>
        </div>
        <div class="title">券包页</div>
        <div class="flex">
          <el-form-item
            class="flex-1"
            label="顶部banner"
            prop="couponTopBanner"
            verify
          >
            <Imgpond
              v-model="form.data.couponTopBanner"
              aspectRatio="375/484"
              :count="1"
            />
          </el-form-item>
          <el-form-item
            class="flex-1"
            label="标题banner"
            prop="couponTitleBanner"
            verify
          >
            <Imgpond
              v-model="form.data.couponTitleBanner"
              aspectRatio="375/120"
              :count="1"
            />
          </el-form-item>
        </div>
      </template>
    </KiFormDialog>

    <!-- 聚合码 -->
    <QrCodePreview ref="qrCode" />
  </div>
</template>

<script>
import pageMixin from '@/utils/pageMixin'
import useAdmateAdapter from '@/utils/useAdmateAdapter'
import QrCodePreview from '@/components/QrCodePreview'
export default {
  components: { QrCodePreview },
  mixins: [pageMixin],
  setup: () =>
    useAdmateAdapter({
      urlPrefix: 'one-travel-api/couponActivityGroup'
    }),
  methods: {
    createQrCode (item) {
      this.$refs.qrCode.show({
        title: item.name + ' - 满减券',
        text: `${import.meta.env.VITE_APP_QRCODE_URL}/couponQRCode?id=${item.id}`
      })
    },
    toActivity (data) {
      this.$router.push({
        path: '/activity/full-coupon/sessions',
        query: { groupId: data.id }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  font-size: 20px;
  margin: 20px 0;
  letter-spacing: 2px;
  font-weight: 600;
}
</style>
